$(function () {
    if (typeof(hs) != 'undefined') {
        hs.align = 'center';
        hs.transitions = ['expand', 'crossfade'];
        hs.outlineType = 'rounded-white';
        hs.fadeInOut = true;
        hs.numberPosition = 'caption';
        hs.dimmingOpacity = 0.75;
        if (hs.addSlideshow)
            hs.addSlideshow({
                // slideshowGroup: 'group1',
                interval: 5000,
                repeat: false,
                useControls: true,
                fixedControls: 'fit',
                overlayOptions: {
                    opacity: .75,
                    position: 'bottom center',
                    hideOnMouseOut: true
                }
            });
    }
});

function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}

/**
 * 判断验证是否通过
 */
function isV(formId) {
    var rs = $("#" + formId).form("validate");
    return rs;
}

function getUrlRelativePath() {
    var url = document.location.toString();
    var arrUrl = url.split("//");
    var start = arrUrl[1].indexOf("/");
    var relUrl = arrUrl[1].substring(start);//stop省略，截取从start开始到结尾的所有字符
    if (relUrl.indexOf("?") != -1) {
        relUrl = relUrl.split("?")[0];
    }
    return relUrl;
}

function doCallback(fn, args) {
    var rs = fn.apply(this, args);
    if (typeof(rs) != 'undefined') {
        return rs;
    }
}

/**
 * 根据list的路径获取详细页路径
 * @param listUrl 列表链接
 * @return 合适的编辑页地址
 */
function getEditUrlByList(listUrl) {
    return "edit.do";
}
/**
 * 根据按钮查找按钮的Gird
 * @param 按钮
 * @return 合适的Grid
 */
function findGrid($btn) {
    var grid = $btn.parent().parent().find("table.easyui-datagrid");
    if (grid != undefined && null != grid && grid.length > 0) {
        grid.gridType = "datagrid";
        return grid;
    }
    grid = $btn.parent().parent().find("table.easyui-treegrid");
    if (grid != undefined && null != grid && grid.length > 0) {
        grid.gridType = "treegrid";
        return grid;
    }
    return null;
}

/**
 * 弹出唱K
 * @param url 路径
 */
function win(url) {
    jrwin(url, 1000, 600);
}

/**
 * 弹出窗口
 * @param url 地址
 * @param w 宽度
 * @param h 高度
 */
function jrwin(url, w, h) {
    var iWidth = w;
    var iHeight = h;
    var iTop = (window.screen.height - 50 - iHeight) / 2;
    var iLeft = (window.screen.width - 10 - iWidth) / 2;
    window
        .open(
        url,
        parseInt(1000 * Math.random()),
        "height="
        + iHeight
        + ",width="
        + iWidth
        + ",top="
        + iTop
        + ",left="
        + iLeft
        + ",toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no");
}

/**
 * 条件查询
 */
function conditionSearch(id) {
    var formControls = $("#" + id + "-cons").find(".form-control");
    var $grid = $("#" + id);
    var param = {};
    var _name = "";
    var _value = "";
    var needRemoveKey = {};
    formControls.each(function () {
        var $this = $(this);
        if ($this.hasClass("easyui-combobox")) {
            _name = $this.attr("comboname");
            _value = $this.combobox("getValue");
        } else if ($this.hasClass("easyui-combotree")) {
            _name = $this.attr("comboname");
            _value = $this.combotree("getValue");
        } else if ($this.hasClass("easyui-numberbox")) {
            _name = $this.attr("comboname");
            _value = $this.numberbox("getValue");
        } else if ($this.hasClass("easyui-datebox")) {
            _name = $this.attr("comboname");
            _value = $this.datebox("getValue");
        } else if ($this.hasClass("easyui-datetimebox")) {
            _name = $this.attr("comboname");
            _value = $this.datetimebox("getValue");
        } else {
            _name = $this.attr("name");
            _value = $this.val();
        }
        if (_value != "") {
            param[_name] = _value;
        } else {//如果值为空的话，需要去除
            needRemoveKey[_name] = true;
        }
    });
    $grid.datagrid("load", mergeGridParam([$grid.datagrid("options").queryParams, param], needRemoveKey));
}

function forFileSize(value, row, index) {
    var fileSize = "";
    var s = row.fileSize / 1024;
    //如果文件大小大于1000，需要使用MB
    if (s > 1000) {
        s = s / 1024;
        fileSize = s.toFixed(2) + "MB";
    } else {
        fileSize = s.toFixed(2) + "KB";
    }
    return fileSize;
}

function getPreviewImg(extension) {
    var img = contextPath + '/admin';
    if (extension == 'doc' || extension == 'docx') {
        img += "/resources/images/word.png";
    } else if (extension == 'ppt' || extension == 'pptx') {
        img += "/resources/images/ppt.png";
    } else if (extension == 'xls' || extension == 'xlsx') {
        img += "/resources/images/excel.png";
    } else if (extension == 'rar' || extension == 'zip') {
        img += "/resources/images/rar.png";
    } else if (extension == 'pdf') {
        img += "/resources/images/pdf.png";
    } else if (extension == 'txt' || extension == 'log') {
        img += "/resources/images/txt.png";
    } else if (extension == 'mp3' || extension == 'wav') {
        img += "/resources/images/music.png";
    } else if (extension == 'mp4' || extension == 'mov' || extension == 'flv') {
        img += "/resources/images/video.png";
    } else {
        img += "/resources/images/cloud.png";
    }
    return img;
}

function forPreview(value, row, index) {
    if (row.fileType == '1' || row.fileType == 1) {
        return "<a href=\""
            + contextPath + row.filePath
            + "\" class=\"highslide\" onclick=\"return hs.expand(this)\"><img src=\""
            + contextPath + row.fileSPath
            + "\" style=\"width:50px;height:50px;margin:5px 0 5px 0;\" /></a>";
    } else {
        return "<img src=\"" + getPreviewImg(row.fileExtension) + "\" style=\"width:50px;height:50px;margin:5px 0 5px 0;\" />";
    }
}

function getParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}

function mergeGridParam(arr, needRemoveKey) {
    var paramObj = {};
    if (null != arr && arr.length > 0) {
        for (var i = 0; i < arr.length; i++) {
            for (var k in arr[i]) {
                if (typeof(needRemoveKey) == 'undefined' || typeof(needRemoveKey[k]) == 'undefined') {
                    paramObj[k] = arr[i][k];
                }
            }
        }
    }
    return paramObj;
}
var paramDetails = null;
function getParamJson(id) {
    if (paramDetails == null) {
        paramDetails = {};
        $.ajax({
            url: contextPath + "/admin/plugin/common-param/paramDetails",
            cache: true,
            async: false,
            type: "post",
            success: function (arr) {
                var obj = {};
                if (null != arr && arr.length > 0) {
                    for (var i = 0; i < arr.length; i++) {
                        paramDetails[arr[i].id] = arr[i].name;
                    }
                }
            }
        });
    }
    return paramDetails[id];
}